<?php

namespace App\Http\Controllers\WorkOrder\Web;

use App\Http\Controllers\BaseController;
use App\Jobs\Export\Export;
use App\Models\Appraise\AppraiseModel;
use App\Service\BaseService;
use App\Service\WorkOrder\Appraise\AppraiseService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class AppraiseController extends BaseController
{


    /**
     * 评价列表
     * @param Request $request
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function lists(Request $request): JsonResponse
    {
        $startTime     = (string)$request->get('startTime', '');
        $endTime       = (string)$request->get('endTime', '');
        $dealStartTime = (string)$request->get('dealStartTime', '');
        $dealEndTime   = (string)$request->get('dealEndTime', '');
        $status        = (int)$request->get('status', 0);
        $score         = (int)$request->get('score', 0);
        $type          = (int)$request->get('type', 0);
        $tag           = (string)$request->get('tag', '');
        $pageSize      = (int)$request->get('pageSize', 20);
        $pageNum       = (int)$request->get('pageNum', 1);
        $export        = (int)$request->get("export", 0);
        $shopInfo      = (string)$request->get('shopId');
        $dealUid       = (int)$request->get("dealUid", 0);
        if ($export) {
            $pageNum  = 1;
            $pageSize = 9999;
        }
        $currentUid = getRmsUserId();
        //导出异步处理
        if ($export) {
            dispatch(
                (new Export(
                    '评价反馈-' . date('Y-m-d') . "-" . time(),
                    Export::TYPE_APPRAISE, [
                        'data' => [
                            'startTime'     => $startTime,
                            'endTime'       => $endTime,
                            'dealStartTime' => $dealStartTime,
                            'dealEndTime'   => $dealEndTime,
                            'status'        => $status,
                            'score'         => $score,
                            'type'          => $type,
                            'tag'           => $tag,
                            'shopInfo'      => $shopInfo,
                            'dealUid'       => $dealUid,
                            'export'        => $export,
                            'pageSize'      => $pageSize,
                            'pageNum'       => $pageNum,
                            'currentUid'    => $currentUid
                        ],
                        'userId'    => $currentUid,
                        'export'    => $export,
                        'startTime' => $startTime,
                        'endTime'   => $endTime,
                    ]
                ))
            )->onQueue(Export::QUEUE_NAME);
            return $this->success([]);
        }
        return $this->success(
            AppraiseService::lists(
                $startTime, $endTime, $dealStartTime, $dealEndTime, $status,
                $score, $type, $tag, $shopInfo, $dealUid, $export, $pageSize, $pageNum, $currentUid
            )
        );
    }


    /**
     * 评价详情
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function info(Request $request, int $id): JsonResponse
    {
        $currentUid = getRmsUserId();
        return $this->success(AppraiseService::info($id, 1, '', 0, $currentUid));
    }

    /**
     * 新增评价回复
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function addReply(Request $request, int $id): JsonResponse
    {
        $desc         = (string)$request->get('desc', '');
        $tel          = (int)$request->get('tel', 0);
        $isSendNotice = (int)$request->get('isSendNotice', 1);//默认1-发送消息同步到群
        if (!$tel && !$desc) {
            return BaseService::throwErr("回复内容不能为空");
        }
        if ($desc && mb_strlen($desc) > 1500) {
            return BaseService::throwErr("回复内容字数不能大于1500字");
        }
        $currentUid = getRmsUserId();
        AppraiseService::addReply($desc, $tel, $id, $currentUid, $isSendNotice);
        return $this->success();
    }

    /**
     * 处理评价-完成
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function update(Request $request, int $id): JsonResponse
    {
        $currentUid = getRmsUserId();
        AppraiseService::update($id, $currentUid);
        return $this->success();
    }

    /**
     * 获取评价对应内容配置
     * @param Request $request
     * @return JsonResponse
     */
    public function config(Request $request): JsonResponse
    {
        return $this->success(AppraiseService::getConfig(AppraiseModel::TYPE_ALL));
    }


    /**
     * 获取评价对应内容配置-new
     * @param Request $request
     * @return JsonResponse
     */
    public function getNewConfig(Request $request): JsonResponse
    {
        return $this->success(AppraiseService::getNewConfig());
    }
}
